package com.example.aaaapp.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.aaaapp.R;
import com.example.aaaapp.api.Api;
import com.example.aaaapp.api.ApiConfig;
import com.example.aaaapp.api.TtitCallBack;
import com.example.aaaapp.utils.KeyboardUtils;
import com.example.aaaapp.utils.ShowUtils;
import com.example.aaaapp.utils.StringUtils;

import java.util.HashMap;

import okhttp3.OkHttpClient;

public class RegisterActivity extends BaseActivity {

    Context mContext;
    private Button btnRegister;
    private EditText etAccount;
    private EditText etPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        mContext = this;
        etAccount = findViewById(R.id.et_account);
        etPwd = findViewById(R.id.et_pwd);
        btnRegister = findViewById(R.id.btn_register);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String account = etAccount.getText().toString().trim();
                String pwd = etPwd.getText().toString().trim();
                register(account, pwd);
            }
        });
    }

    void register(String account, String pwd) {
        if (StringUtils.isEmpyt(account)) {
            ShowUtils.showMessage(this, "账号不能为空");
            return;
        }
        if (StringUtils.isEmpyt(pwd)) {
            ShowUtils.showMessage(this, "密码不能为空");
            return;
        }
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        HashMap<String, Object> p = new HashMap<>();
        p.put("account", account);
        p.put("pwd", pwd);

        Api.config(ApiConfig.RegisterUrl, p).postRequest(this, new TtitCallBack() {
            @Override
            public void onSuccess(String res) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showMessage(res);
                    }
                });
            }

            @Override
            public void onFailure(Exception e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showMessage(e.getMessage());
                    }
                });
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        etAccount.endBatchEdit();
        etPwd.endBatchEdit();
        return super.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            KeyboardUtils.hideKeyboard(ev, view, this);
        }
        return super.dispatchTouchEvent(ev);
    }
}